package com.compass.doctor.ui.consultation.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.compass.doctor.R
import com.compass.doctor.databinding.LayoutFoundingItemBinding
import com.compass.doctor.databinding.LayoutInitiateItemBinding
import com.compass.framework.adapter.BaseBindViewHolder
import com.compass.framework.adapter.BaseMultiItemAdapter
import com.compass.framework.ext.gone
import com.compass.framework.ext.saveAs
import com.compass.framework.ext.visible
import com.compass.framework.glide.setUrlCircle
import com.compass.framework.manager.DictionariesManager
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.ConsultationOrder
import com.compass.framework.model.Doctor
import com.compass.framework.utils.BigDecimalUtils
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.getDrawableResource

/**
 * Description： 会诊列表adapter
 * Date:2023/12/21 15:38
 */
class ConsultationAdapter constructor(private var itemType: Int) :
    BaseMultiItemAdapter<ConsultationOrder>() {


    private lateinit var consultationDoctorAdapter: ConsultationDoctorAdapter

    //用来实现多item布局
    override fun getDefItemViewType(position: Int): Int {
        return itemType
    }

    override fun onBindDefViewHolder(
        holder: BaseBindViewHolder<ViewBinding>,
        item: ConsultationOrder?,
        position: Int
    ) {
        if (item == null) return
        //  //1、发起的， 2、加入的
        when (holder.itemViewType) {
            1 -> {
                val binding = holder.binding.saveAs<LayoutFoundingItemBinding>()
                binding.apply {
                    tvName.text = item.patientName
                    ivHead.setUrlCircle(item.patientAvatar)
                    tvAge.text = item.patientAge
                    ivSex.background =
                        if (item.patientSex == 1) getDrawableResource(R.mipmap.icon_male) else getDrawableResource(
                            R.mipmap.icon_female
                        )
                    tvStatus.text =
                        DictionariesManager.getConsultationStatusValue(item.status.toString())
                    tvStartTime.text = DateUtils.conversionTime(item.dateTime, DateUtils.FORMAT_TWO)

                    rcyDoctors.layoutManager = LinearLayoutManager(
                        binding.rcyDoctors.context,
                        LinearLayoutManager.HORIZONTAL,
                        false
                    )
                    consultationDoctorAdapter = ConsultationDoctorAdapter(1)
                    rcyDoctors.adapter = consultationDoctorAdapter
                    consultationDoctorAdapter.setData(item.doctors)
                    when (item.status) {
                        2 -> {
                            tvButtonStatus.text = "进入会诊"
                            tvButtonStatus.background =
                                getDrawableResource(R.drawable.btn_shape_green_50)
                            tvButtonStatus.visible()

                        }

                        3 -> {
                            tvButtonStatus.text = "查看记录"
                            tvButtonStatus.background =
                                getDrawableResource(R.drawable.btn_shape_blue_50)
                            tvButtonStatus.visible()
                        }

                        else -> {
                            tvButtonStatus.gone()
                        }

                    }

                }
            }

            else -> {
                val binding = holder.binding.saveAs<LayoutInitiateItemBinding>()
                binding.apply {
                    tvName.text = item.patientName
                    ivHead.setUrlCircle(item.patientAvatar)
                    tvAge.text = item.patientAge
                    ivSex.background =
                        if (item.patientSex == 1) getDrawableResource(R.mipmap.icon_male) else getDrawableResource(
                            R.mipmap.icon_female
                        )
//                    tvStatus.text = DateUtils.conversionTime(item.createTime, DateUtils.FORMAT_TWO)
                    tvStartTime.text = DateUtils.conversionTime(item.dateTime, DateUtils.FORMAT_TWO)
                    tvStatus.text =
                        DictionariesManager.getConsultationStatusValue(item.status.toString())
                    val doctor = item.creator
                    doctor?.let {
                        tvInviteName.text = it.doctorName
                        ivInviteHead.setUrlCircle(it.doctorAvatar)
                        tvHospitalFee.text = BigDecimalUtils.divide(it.price)
                    }
                    when (item.status) {
                        4 -> {
                            tvButtonStatus.visible()
                            if (getMyDoctorStatus(item.doctors) == 0) {
                                tvButtonStatus.text = "立即加入"
                                tvButtonStatus.background =
                                    getDrawableResource(R.drawable.btn_shape_blue_50)
                            } else {
                                tvButtonStatus.text = "已同意"
                                tvButtonStatus.background =
                                    getDrawableResource(R.drawable.btn_shape_green_50)
                            }
                        }

                        2 -> {
                            tvButtonStatus.text = "进入会诊"
                            tvButtonStatus.background =
                                getDrawableResource(R.drawable.btn_shape_green_50)
                            tvButtonStatus.visible()
                        }

                        else -> {
                            tvButtonStatus.gone()
                        }

                    }
                }

            }
        }
    }

    override fun getViewBinding(
        layoutInflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            1 -> {
                LayoutFoundingItemBinding.inflate(layoutInflater, parent, false)
            }


            else -> {
                LayoutInitiateItemBinding.inflate(layoutInflater, parent, false)
            }
        }

    }

    private fun getMyDoctorStatus(doctors: MutableList<Doctor>): Int {
        for (item in doctors) {
            if (item.doctorId == UserInfoManager.getUserInfo()?.id) {
                return item.status
            }
        }
        return 10086
    }
}